/*
 * CS 567 GUI - Semester Project - "DecisionLists"
 * Jason Frank
 * Spring 2011
 *
 * This File:  Handles the input buttons "Add Item" and "Edit List" (potentially others too) at btm of app.
 *
 */

package Decision_Lists;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

/*
 */
//public class Add_Edit_View extends JPanel  {
public class Add_Edit_View extends Panel_Fader_User  {

  //boolean is_center_view = true;
  DL_Model model;
  final String view_name = "Add_Edit_View";
  int panelWidthPref  = 725;  // probably ignored
  int panelHeightPref = 40;
  DL_ViewInterface addItemView;  // needed to pass the approp view ref to model upon "Add Item" btn click
  DL_ViewInterface addItemForm;

  boolean inAddItemState = false;

  JPanel addItemBtnPanel = new JPanel();
  JPanel saveItemBtnPanel = new JPanel();

  JButton addItemBtn  = new JButton("Add Item");
  JButton saveItemBtn = new JButton("Save Item & Return");
  JButton cancelBtn = new JButton("Cancel & Return");
  DL_ViewInterface savedView;

  ////// end Class Vars /////////
  ///////////////////////////////
  
  // Constructor #1
  public Add_Edit_View( final DL_Model model, final AddItemView addItemView ) throws IOException, Exception  {

    this.model = model;

    this.addItemView = addItemView;

    layoutPanel();
    // Setup the panel fader
    super.init_PanelFaderUser(this, addItemBtnPanel);
  } ////// end constructor #1 //////

  // Constructor #2
  public Add_Edit_View( final DL_Model model, final AddItemForm addItemForm ) throws IOException, Exception  {

    this.model = model;

    this.addItemForm = addItemForm;

    layoutPanel();
    // Setup the panel fader
    super.init_PanelFaderUser(this, addItemBtnPanel);
  } ////// end constructor #2 //////


  protected void layoutPanel() {
    // <editor-fold defaultstate="collapsed" desc="method contents">
    //// LAYOUT ////
    setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

    addItemBtn.setAlignmentX(LEFT_ALIGNMENT);
    saveItemBtn.setAlignmentX(LEFT_ALIGNMENT);

    addItemBtn.addActionListener(new addBtnActionListener());
    saveItemBtn.addActionListener(new saveBtnActionListener());
    cancelBtn.addActionListener(new cancelBtnActionListener());


    Box addItemHorizBox = Box.createHorizontalBox();
    addItemHorizBox.add(Box.createRigidArea(new Dimension(15, 0)));
    addItemHorizBox.add(addItemBtn);
    addItemHorizBox.add(Box.createHorizontalGlue());

    addItemBtnPanel.setLayout(new BoxLayout(addItemBtnPanel, BoxLayout.X_AXIS));
    addItemBtnPanel.add(Box.createRigidArea(new Dimension(15, 0)));
    addItemBtnPanel.add(addItemHorizBox);
    // addItemBtnPanel.setAlignmentX(SwingConstants.LEFT);

    Box saveItemHorizBox = Box.createHorizontalBox();
    saveItemHorizBox.add(Box.createRigidArea(new Dimension(15, 0)));
    saveItemHorizBox.add(saveItemBtn);
    saveItemHorizBox.add(Box.createRigidArea(new Dimension(15, 0)));
    saveItemHorizBox.add(cancelBtn);
    saveItemHorizBox.add(Box.createHorizontalGlue());

    saveItemBtnPanel.setLayout(new BoxLayout(saveItemBtnPanel, BoxLayout.X_AXIS));
    saveItemBtnPanel.add(Box.createRigidArea(new Dimension(15, 0)));
    saveItemBtnPanel.add(saveItemHorizBox);// </editor-fold>
  } ////// end layoutPanel() /////////

  protected class addBtnActionListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
      //System.out.println("You clicked the Add Item btn");

      // Reset the form so user starts with empty fields, etc (removes previous item's stuff)
      ((AddItemForm)addItemForm).resetComponents();
      // Save the center view that the user was on so we can return them to that view 
      savedView = model.getCurView();

      model.setCurView(addItemForm);  // The model then notifies the views, so Center_Panel fades to the AddItemForm
      
      // FADE TO THE OTHER BUTTON PANEL
      pf3.setFadein_panel(saveItemBtnPanel);
      try {
        pf3.startFade();
      } catch (Exception ex) {
        Logger.getLogger(Add_Edit_View.class.getName()).log(Level.SEVERE, null, ex);
      }
      inAddItemState = true;  // transitioning to the Add Item page
    }
  } //////// end nested class addBtnActionListener ///////////

  protected class saveBtnActionListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {

      // Add new item to model using data from the input widgets of the AddItemForm
      try {
        ((AddItemForm) addItemForm).saveItemToModel();
      } catch (IOException ex) {
        Logger.getLogger(Add_Edit_View.class.getName()).log(Level.SEVERE, null, ex);
      }
      // Return the user to the view they were on before adding a list item
      model.setCurView(savedView);
      // Fade to the other btn panel
      pf3.setFadein_panel(addItemBtnPanel);
      try {
        pf3.startFade();
      } catch (Exception ex) {
        Logger.getLogger(Add_Edit_View.class.getName()).log(Level.SEVERE, null, ex);
      }
      inAddItemState = false; // transitioning out of the Add Item page, back into the normal views
    }
  } //////// end nested class saveBtnActionListener ///////////

  
  protected class cancelBtnActionListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {

      model.setCurView(savedView);
      pf3.setFadein_panel(addItemBtnPanel);
      try {
        pf3.startFade();
      } catch (Exception ex) {
        Logger.getLogger(Add_Edit_View.class.getName()).log(Level.SEVERE, null, ex);
      }
      inAddItemState = false; // transitioning out of the Add Item page, back into the normal views
    }
  } //////// end nested class cancelBtnActionListener ///////////


  @Override
  public Dimension getMinimumSize()   { return getPreferredSize(); }
  @Override
  public Dimension getPreferredSize() { return new Dimension(panelWidthPref, panelHeightPref); }


} //////////////////// end class Add_Edit_View /////////////////////////////
